{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   P00000142  P00000242  P00000342  P00000442  P00000542  P00000642  \\\n",
      "0       True      False      False      False      False      False   \n",
      "1      False      False      False      False      False      False   \n",
      "2      False      False      False      False      False      False   \n",
      "3      False      False      False      False      False      False   \n",
      "4      False      False      False      False      False      False   \n",
      "\n",
      "   P00000742  P00000842  P00000942  P00001042    ...     P0098942  P0099042  \\\n",
      "0      False      False      False      False    ...        False     False   \n",
      "1      False      False      False      False    ...        False     False   \n",
      "2      False      False      False      False    ...        False     False   \n",
      "3      False      False      False      False    ...        False     False   \n",
      "4      False      False      False      False    ...        False     False   \n",
      "\n",
      "   P0099142  P0099242  P0099342  P0099442  P0099642  P0099742  P0099842  \\\n",
      "0     False     False     False     False     False     False     False   \n",
      "1     False     False     False     False     False     False     False   \n",
      "2     False     False     False     False     False     False     False   \n",
      "3     False     False     False     False     False     False     False   \n",
      "4     False     False     False     False     False     False     False   \n",
      "\n",
      "   P0099942  \n",
      "0     False  \n",
      "1     False  \n",
      "2     False  \n",
      "3     False  \n",
      "4     False  \n",
      "\n",
      "[5 rows x 3623 columns]\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "df=pd.read_csv(\"D:\\study\\data\\project\\cleaned.csv\")\n",
    "lst=[]\n",
    "for item in df['User_ID'].unique():\n",
    "    lst2=list(set(df[df['User_ID']==item]['Product_ID']))\n",
    "    if len(lst2)>0:\n",
    "        lst.append(lst2)\n",
    "        from mlxtend.preprocessing import TransactionEncoder\n",
    "from mlxtend.frequent_patterns import apriori, association_rules\n",
    "\n",
    "te=TransactionEncoder()\n",
    "te_data=te.fit(lst).transform(lst)\n",
    "df_x=pd.DataFrame(te_data,columns=te.columns_)\n",
    "print(df_x.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       support                           itemsets\n",
      "0     0.191818                        (P00000142)\n",
      "1     0.062977                        (P00000242)\n",
      "2     0.086912                        (P00000642)\n",
      "3     0.083857                        (P00001042)\n",
      "4     0.097097                        (P00001142)\n",
      "5     0.059243                        (P00001642)\n",
      "6     0.076218                        (P00001742)\n",
      "7     0.123578                        (P00002142)\n",
      "8     0.055169                        (P00002442)\n",
      "9     0.090307                        (P00002542)\n",
      "10    0.140893                        (P00003242)\n",
      "11    0.161433                        (P00003442)\n",
      "12    0.085384                        (P00003642)\n",
      "13    0.123918                        (P00003942)\n",
      "14    0.100323                        (P00004742)\n",
      "15    0.162112                        (P00005042)\n",
      "16    0.084026                        (P00006942)\n",
      "17    0.059243                        (P00009342)\n",
      "18    0.059073                        (P00010242)\n",
      "19    0.225938                        (P00010742)\n",
      "20    0.112375                        (P00010842)\n",
      "21    0.051434                        (P00010942)\n",
      "22    0.059413                        (P00013742)\n",
      "23    0.112884                        (P00014542)\n",
      "24    0.053132                        (P00014642)\n",
      "25    0.069088                        (P00014842)\n",
      "26    0.069598                        (P00015642)\n",
      "27    0.075030                        (P00016042)\n",
      "28    0.052453                        (P00016842)\n",
      "29    0.050246                        (P00018042)\n",
      "...        ...                                ...\n",
      "1891  0.065693             (P00265242, P00258742)\n",
      "1892  0.056187             (P00265242, P00259342)\n",
      "1893  0.051265             (P00260042, P00265242)\n",
      "1894  0.067900             (P00270942, P00265242)\n",
      "1895  0.053302             (P00271142, P00265242)\n",
      "1896  0.058903             (P00274942, P00265242)\n",
      "1897  0.067391             (P00265242, P00277642)\n",
      "1898  0.100492             (P00278642, P00265242)\n",
      "1899  0.057715             (P00289942, P00265242)\n",
      "1900  0.051434             (P00293242, P00265242)\n",
      "1901  0.050755             (P00265242, P00294542)\n",
      "1902  0.062468             (P00295942, P00265242)\n",
      "1903  0.057206             (P00265242, P00296042)\n",
      "1904  0.055339             (P00317842, P00265242)\n",
      "1905  0.051265             (P00318742, P00265242)\n",
      "1906  0.050925             (P00323942, P00265242)\n",
      "1907  0.058903             (P00324942, P00265242)\n",
      "1908  0.072993             (P00334242, P00265242)\n",
      "1909  0.050246             (P00338442, P00265242)\n",
      "1910  0.056187             (P00355142, P00265242)\n",
      "1911  0.057545              (P0097242, P00265242)\n",
      "1912  0.051944             (P00277442, P00270942)\n",
      "1913  0.054999             (P00270942, P00295942)\n",
      "1914  0.056357             (P00329542, P00270942)\n",
      "1915  0.050586             (P00334242, P00270942)\n",
      "1916  0.061450              (P0097242, P00270942)\n",
      "1917  0.055339             (P00278642, P00334242)\n",
      "1918  0.053302             (P00334242, P00338442)\n",
      "1919  0.050246             (P00334242, P00355142)\n",
      "1920  0.052962  (P00112142, P00110742, P00025442)\n",
      "\n",
      "[1921 rows x 2 columns]\n"
     ]
    }
   ],
   "source": [
    "frequent_items=apriori(df_x,use_colnames=True,min_support=0.05)\n",
    "print(frequent_items)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     antecedents consequents  antecedent support  consequent support  \\\n",
      "2287   P00193542   P00120042            0.102869            0.151417   \n",
      "2286   P00120042   P00193542            0.151417            0.102869   \n",
      "2607   P00250242   P00248142            0.128841            0.137328   \n",
      "2606   P00248142   P00250242            0.137328            0.128841   \n",
      "1161   P00120042   P00057942            0.151417            0.131387   \n",
      "1160   P00057942   P00120042            0.131387            0.151417   \n",
      "1394   P00140742   P00073842            0.132575            0.156340   \n",
      "1395   P00073842   P00140742            0.156340            0.132575   \n",
      "1515   P00086442   P00323942            0.162281            0.133424   \n",
      "1514   P00323942   P00086442            0.133424            0.162281   \n",
      "2150   P00329542   P00114942            0.117977            0.197250   \n",
      "2151   P00114942   P00329542            0.197250            0.117977   \n",
      "2458   P00154042   P00270942            0.104397            0.193685   \n",
      "2459   P00270942   P00154042            0.193685            0.104397   \n",
      "1370   P00071442   P00086442            0.123069            0.162281   \n",
      "1371   P00086442   P00071442            0.162281            0.123069   \n",
      "1949   P00329542   P00111142            0.117977            0.170429   \n",
      "1948   P00111142   P00329542            0.170429            0.117977   \n",
      "1007   P00052842   P00323942            0.164318            0.133424   \n",
      "1006   P00323942   P00052842            0.133424            0.164318   \n",
      "1169    P0097242   P00057942            0.152096            0.131387   \n",
      "1168   P00057942    P0097242            0.131387            0.152096   \n",
      "2353   P00270942   P00140742            0.193685            0.132575   \n",
      "2352   P00140742   P00270942            0.132575            0.193685   \n",
      "2703   P00270942   P00329542            0.193685            0.117977   \n",
      "2702   P00329542   P00270942            0.117977            0.193685   \n",
      "2146   P00277442   P00114942            0.109998            0.197250   \n",
      "2147   P00114942   P00277442            0.197250            0.109998   \n",
      "731    P00044442   P00070042            0.186556            0.124088   \n",
      "730    P00070042   P00044442            0.124088            0.186556   \n",
      "...          ...         ...                 ...                 ...   \n",
      "1353   P00265242   P00062842            0.315396            0.142251   \n",
      "1352   P00062842   P00265242            0.142251            0.315396   \n",
      "2027   P00278642   P00112142            0.205907            0.261246   \n",
      "2026   P00112142   P00278642            0.261246            0.205907   \n",
      "1467   P00085242   P00265242            0.151587            0.315396   \n",
      "1466   P00265242   P00085242            0.315396            0.151587   \n",
      "2263   P00265242   P00117942            0.315396            0.228484   \n",
      "2262   P00117942   P00265242            0.228484            0.315396   \n",
      "1759   P00110742   P00278642            0.270073            0.205907   \n",
      "1758   P00278642   P00110742            0.205907            0.270073   \n",
      "1459   P00265242   P00080342            0.315396            0.201324   \n",
      "1458   P00080342   P00265242            0.201324            0.315396   \n",
      "2663   P00265242   P00270942            0.315396            0.193685   \n",
      "2662   P00270942   P00265242            0.193685            0.315396   \n",
      "445    P00025442   P00278642            0.269224            0.205907   \n",
      "444    P00278642   P00025442            0.205907            0.269224   \n",
      "2331   P00265242   P00128942            0.315396            0.157189   \n",
      "2330   P00128942   P00265242            0.157189            0.315396   \n",
      "2143   P00114942   P00265242            0.197250            0.315396   \n",
      "2142   P00265242   P00114942            0.315396            0.197250   \n",
      "2594   P00242742   P00265242            0.202682            0.315396   \n",
      "2595   P00265242   P00242742            0.315396            0.202682   \n",
      "1575   P00265242   P00102642            0.315396            0.208454   \n",
      "1574   P00102642   P00265242            0.208454            0.315396   \n",
      "1511   P00265242   P00086442            0.315396            0.162281   \n",
      "1510   P00086442   P00265242            0.162281            0.315396   \n",
      "435    P00025442   P00265242            0.269224            0.315396   \n",
      "434    P00265242   P00025442            0.315396            0.269224   \n",
      "2019   P00265242   P00112142            0.315396            0.261246   \n",
      "2018   P00112142   P00265242            0.261246            0.315396   \n",
      "\n",
      "       support  confidence      lift  leverage  conviction  \n",
      "2287  0.051095    0.496700  3.280334  0.035519    1.686036  \n",
      "2286  0.051095    0.337444  3.280334  0.035519    1.354046  \n",
      "2607  0.051265    0.397892  2.897381  0.033571    1.432753  \n",
      "2606  0.051265    0.373300  2.897381  0.033571    1.390075  \n",
      "1161  0.053981    0.356502  2.713378  0.034086    1.349831  \n",
      "1160  0.053981    0.410853  2.713378  0.034086    1.440357  \n",
      "1394  0.056018    0.422535  2.702666  0.035291    1.460972  \n",
      "1395  0.056018    0.358306  2.702666  0.035291    1.351774  \n",
      "1515  0.057376    0.353556  2.649874  0.035723    1.340529  \n",
      "1514  0.057376    0.430025  2.649874  0.035723    1.469747  \n",
      "2150  0.061110    0.517986  2.626035  0.037839    1.665407  \n",
      "2151  0.061110    0.309811  2.626035  0.037839    1.277944  \n",
      "2458  0.052792    0.505691  2.610890  0.032572    1.631196  \n",
      "2459  0.052792    0.272568  2.610890  0.032572    1.231185  \n",
      "1370  0.051944    0.422069  2.600845  0.031972    1.449513  \n",
      "1371  0.051944    0.320084  2.600845  0.031972    1.289763  \n",
      "1949  0.051944    0.440288  2.583402  0.031837    1.482138  \n",
      "1948  0.051944    0.304781  2.583402  0.031837    1.268698  \n",
      "1007  0.054999    0.334711  2.508627  0.033075    1.302555  \n",
      "1006  0.054999    0.412214  2.508627  0.033075    1.421744  \n",
      "1169  0.050076    0.329241  2.505890  0.030093    1.294971  \n",
      "1168  0.050076    0.381137  2.505890  0.030093    1.370099  \n",
      "2353  0.063656    0.328659  2.479040  0.037979    1.292078  \n",
      "2352  0.063656    0.480154  2.479040  0.037979    1.551064  \n",
      "2703  0.056357    0.290973  2.466361  0.033507    1.243991  \n",
      "2702  0.056357    0.477698  2.466361  0.033507    1.543771  \n",
      "2146  0.053471    0.486111  2.464441  0.031774    1.562108  \n",
      "2147  0.053471    0.271084  2.464441  0.031774    1.220994  \n",
      "731   0.056866    0.304823  2.456511  0.033717    1.259984  \n",
      "730   0.056866    0.458276  2.456511  0.033717    1.501585  \n",
      "...        ...         ...       ...       ...         ...  \n",
      "1353  0.050586    0.160388  1.127497  0.005720    1.021601  \n",
      "1352  0.050586    0.355609  1.127497  0.005720    1.062403  \n",
      "2027  0.060601    0.294312  1.126569  0.006808    1.046856  \n",
      "2026  0.060601    0.231969  1.126569  0.006808    1.033933  \n",
      "1467  0.053811    0.354983  1.125515  0.006001    1.061373  \n",
      "1466  0.053811    0.170614  1.125515  0.006001    1.022940  \n",
      "2263  0.080971    0.256728  1.123613  0.008908    1.037999  \n",
      "2262  0.080971    0.354383  1.123613  0.008908    1.060387  \n",
      "1759  0.062129    0.230044  1.117221  0.006519    1.031348  \n",
      "1758  0.062129    0.301731  1.117221  0.006519    1.045338  \n",
      "1459  0.070786    0.224435  1.114794  0.007289    1.029799  \n",
      "1458  0.070786    0.351602  1.114794  0.007289    1.055839  \n",
      "2663  0.067900    0.215285  1.111521  0.006813    1.027526  \n",
      "2662  0.067900    0.350570  1.111521  0.006813    1.054160  \n",
      "445   0.061280    0.227617  1.105433  0.005845    1.028107  \n",
      "444   0.061280    0.297609  1.105433  0.005845    1.040412  \n",
      "2331  0.054660    0.173305  1.102524  0.005083    1.019494  \n",
      "2330  0.054660    0.347732  1.102524  0.005083    1.049574  \n",
      "2143  0.068409    0.346816  1.099619  0.006197    1.048102  \n",
      "2142  0.068409    0.216900  1.099619  0.006197    1.025092  \n",
      "2594  0.070277    0.346734  1.099358  0.006352    1.047970  \n",
      "2595  0.070277    0.222820  1.099358  0.006352    1.025912  \n",
      "1575  0.072144    0.228741  1.097321  0.006398    1.026304  \n",
      "1574  0.072144    0.346091  1.097321  0.006398    1.046941  \n",
      "1511  0.055848    0.177072  1.091142  0.004665    1.017973  \n",
      "1510  0.055848    0.344142  1.091142  0.004665    1.043829  \n",
      "435   0.088440    0.328499  1.041545  0.003528    1.019513  \n",
      "434   0.088440    0.280409  1.041545  0.003528    1.015543  \n",
      "2019  0.085045    0.269645  1.032149  0.002649    1.011500  \n",
      "2018  0.085045    0.325536  1.032149  0.002649    1.015034  \n",
      "\n",
      "[2720 rows x 9 columns]\n"
     ]
    }
   ],
   "source": [
    "rules=association_rules(frequent_items,metric='lift',min_threshold=1)\n",
    "rules.antecedents=rules.antecedents.apply(lambda x: next(iter(x)))\n",
    "rules.consequents=rules.consequents.apply(lambda x: next(iter(x)))\n",
    "rules=rules.sort_values('lift',ascending=False)\n",
    "print(rules)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<script type=\"text/javascript\">window.PlotlyConfig = {MathJaxConfig: 'local'};</script><script type=\"text/javascript\">if (window.MathJax) {MathJax.Hub.Config({SVG: {font: \"STIX-Web\"}});}</script><script>requirejs.config({paths: { 'plotly': ['https://cdn.plot.ly/plotly-latest.min']},});if(!window._Plotly) {require(['plotly'],function(plotly) {window._Plotly=plotly;});}</script>"
      ],
      "text/vnd.plotly.v1+html": [
       "<script type=\"text/javascript\">window.PlotlyConfig = {MathJaxConfig: 'local'};</script><script type=\"text/javascript\">if (window.MathJax) {MathJax.Hub.Config({SVG: {font: \"STIX-Web\"}});}</script><script>requirejs.config({paths: { 'plotly': ['https://cdn.plot.ly/plotly-latest.min']},});if(!window._Plotly) {require(['plotly'],function(plotly) {window._Plotly=plotly;});}</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'file://D:\\\\program\\\\python\\\\Scripts\\\\association_rules.html'"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import plotly.plotly as py\n",
    "import plotly.graph_objs as go\n",
    "from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot\n",
    "\n",
    "init_notebook_mode(connected=True)\n",
    "\n",
    "import networkx as nx\n",
    "\n",
    "nx_data=rules[rules.lift>=2]\n",
    "GA=nx.from_pandas_edgelist(nx_data,source='antecedents',target='consequents',edge_attr='lift')\n",
    "pos=nx.kamada_kawai_layout(GA,weight='lift')\n",
    "# pos = nx.nx_agraph.graphviz_layout(GA)\n",
    "# pos = nx.nx_agraph.graphviz_layout(GA, prog='dot')\n",
    "\n",
    "edge_trace = go.Scatter(\n",
    "    x=[],\n",
    "    y=[],\n",
    "    line=dict(width=0.5,color='#888'),\n",
    "    hoverinfo='none',\n",
    "    mode='lines')\n",
    "\n",
    "for edge in GA.edges():\n",
    "    x0, y0 = pos[edge[0]]\n",
    "    x1, y1 = pos[edge[1]]\n",
    "    edge_trace['x'] += tuple([x0, x1, None])\n",
    "    edge_trace['y'] += tuple([y0, y1, None])\n",
    "\n",
    "node_trace = go.Scatter(\n",
    "    x=[],\n",
    "    y=[],\n",
    "    text=[],\n",
    "    mode='markers',\n",
    "    hoverinfo='text',\n",
    "    marker=dict(\n",
    "        showscale=True,\n",
    "        colorscale='YlGnBu',\n",
    "        reversescale=True,\n",
    "        color=[],\n",
    "        size=10,\n",
    "        colorbar=dict(\n",
    "            thickness=15,\n",
    "            title='Node Connections',\n",
    "            xanchor='left',\n",
    "            titleside='right'\n",
    "        ),\n",
    "        line=dict(width=2)))\n",
    "\n",
    "for node in GA.nodes():\n",
    "    x, y = pos[node]\n",
    "    node_trace['x'] += tuple([x])\n",
    "    node_trace['y'] += tuple([y])\n",
    "\n",
    "for node,adjacencies in enumerate(GA.adjacency()):\n",
    "    node_trace['marker']['color']+=tuple([len(adjacencies[1])])\n",
    "    node_info = str(adjacencies[0])+' - # of connections: '+str(len(adjacencies[1]))\n",
    "    node_trace['text']+=tuple([node_info])\n",
    "    \n",
    "fig = go.Figure(data=[edge_trace, node_trace],\n",
    "             layout=go.Layout(\n",
    "                title='<br>association_rules graph',\n",
    "                titlefont=dict(size=16),\n",
    "                showlegend=False,\n",
    "                hovermode='closest',\n",
    "                margin=dict(b=20,l=5,r=5,t=40),\n",
    "                annotations=[ dict(\n",
    "                    text=\"Python code: <a href='https://plot.ly/ipython-notebooks/network-graphs/'> https://plot.ly/ipython-notebooks/network-graphs/</a>\",\n",
    "                    showarrow=False,\n",
    "                    xref=\"paper\", yref=\"paper\",\n",
    "                    x=0.005, y=-0.002 ) ],\n",
    "                xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),\n",
    "                yaxis=dict(showgrid=False, zeroline=False, showticklabels=False)))\n",
    "plot(fig, filename='association_rules.html')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.DataFrame(frequent_items).to_csv('frequent_items.csv')\n",
    "pd.DataFrame(rules).to_csv('rules.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
